/**
 * 判断数组是否包含指定的元素
 *
 * @param {*} array       数组
 * @param {*} elements    元素
 * @returns 返回ture表示包含，否则返回false表示不包含
 */
export function containsAny(array, elements) {
  if (!array) {
    return false
  }
  if (!elements) {
    return false
  }
  if (elements instanceof Array) {
    let flag = false
    for (const i in elements) {
      flag = flag | array.includes(elements[i])
      if (flag) {
        break
      }
    }
    return flag
  } else {
    return array.includes(elements)
  }
}

/**
 * 判断数组是否包含指定的全部元素
 *
 * @param {*} array       数组
 * @param {*} elements    元素
 * @returns 返回ture表示包含，否则返回false表示不包含
 */
export function containsAll(array, elements) {
  if (!array) {
    return false
  }
  if (!elements) {
    return false
  }
  if (elements instanceof Array) {
    let flag = true
    for (const i in elements) {
      flag = flag & array.includes(elements[i])
      if (!flag) {
        break
      }
    }
    return flag
  } else {
    return array.includes(elements)
  }
}

/**
 * 把数组转换成字符串，以指定的分隔符分割
 *
 * @param {*} array       数组
 * @param {*} func        访问数组的元素，返回组成输出字符串的元素
 * @param {*} splitter    分隔符，不指定缺省情况下为：,
 * @returns               输出字符串
 */
export function toString(array, func, splitter) {
  if (array && array instanceof Array) {
    let s
    if (splitter) {
      s = splitter
    } else {
      s = ','
    }
    let value = ''
    array.forEach(e => {
      if (func && func instanceof Function) {
        const item = func(e)
        if (item) {
          value += item + s
        } else {
          value += s
        }
      } else {
        value += e + s
      }
    })
    if (value.length > 0) {
      value = value.substring(0, value.length - s.length)
    }
    return value
  }
}

/**
 * 格式化树状列表，用于ElementUI的Table的树状显示
 *
 * @param {*} list                源数据集合
 * @param {*} childrenField       子节点字段名
 * @param {*} elementAction       主元素上的操作
 * @param {*} subElementAction    子元素上的操作
 * @returns                       返回树状的数据集合
 */
export function formatTreeRow(list, childrenField, elementAction, subElementAction, onlySubElementAction) {
  const array = []
  list = list || []
  elementAction = elementAction || function(e, p) {}
  subElementAction = subElementAction || function(s, p, c) {}
  onlySubElementAction = onlySubElementAction || function(s, p, c) {}
  for (let i = 0, len_i = list.length; i < len_i; i++) {
    const parent = list[i]
    const children = parent[childrenField]
    if (children) {
      let element = null
      let subElement = null
      for (let j = 0, len_j = children.length; j < len_j; j++) {
        if (j === 0) {
          element = {}
          elementAction(element, parent)
          element = element || {}
          subElementAction(element, parent, children[j])
          element = element || {}
          element.rowKey = i + '_' + j
          element[childrenField] = []
          array.push(element)
        } else {
          subElement = {}
          subElementAction(subElement, parent, children[j])
          onlySubElementAction(subElement, parent, children[j])
          subElement = subElement || {}
          subElement.rowKey = i + '_' + j
          element[childrenField].push(subElement)
        }
      }
    }
  }
  return array
}

export function formatEmbedRow(list, childrenField) {
  list = list || []
  for (let i = 0, len_i = list.length; i < len_i; i++) {
    const parent = list[i]
    parent.rowKey = i + '_'
    const children = parent[childrenField]
    if (children) {
      for (let j = 0, len_j = children.length; j < len_j; j++) {
        const child = children[j]
        child.rowKey = i + '_' + j
      }
    }
  }
  return list
}

